package com.example.clxy.service.impl;

import com.example.clxy.dao.PlatformMapper;
import com.example.clxy.entity.Platform;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.PageResult.PlatformPageResult;
import com.example.clxy.result.business.PlatformResult;
import com.example.clxy.service.PlatformService;
import com.example.clxy.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author liuhaoqi
 * @Date 2020/8/4 20:07
 * @Version 1.0
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PlatformServiceImpl implements PlatformService {

    @Autowired
    PlatformMapper platformMapper;
    @Override
    public WebRestResult addPlatform(Platform platform) {
        WebRestResult result = new WebRestResult();
        platform.setPlatformid(UUIDUtil.randomUUID());
        platform.setCreatedTime(new Date());
        platform.setUpdatedTime(new Date());
        platform.setFlag("0");
        if (platformMapper.insertSelective(platform) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("新增平台基本信息成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增平台基本信息失败！");
        }
        return result;
    }

    @Override
    public WebRestResult updatePlatform(Platform platform) {
        WebRestResult result = new WebRestResult();
        platform.setUpdatedTime(new Date());
        if (platformMapper.updateByPrimaryKeySelective(platform) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("更新平台基本信息成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新平台基本信息失败！");
        }
        return result;
    }

    @Override
    public WebRestResult deletePlatform(String platformid) {
        WebRestResult result = new WebRestResult();
        if (platformMapper.deletePlatform(platformid) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除平台基本信息成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除平台基本信息失败！");
        }
        return result;
    }

    @Override
    public PageResult<PlatformPageResult> getPlatformList(String platformname, String platformintro,
                                                          String credit, String platformhour, int page, int pagesize,String status) {
        PageResult<PlatformPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        if (status!=null&&status.equals("0")) {
            List<PlatformPageResult> list = platformMapper.getPlatformListSimple(platformname, platformintro, credit,
                    platformhour, begin, end, pagesize);
            if (list.size() > 0) {
                result.setTotal(list.get(0).getCnt());
                result.setPageCount(list.get(0).getPage());
                result.setPageSize(pagesize);
                result.setPageIndex(page);
                result.setRows(list);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("获取平台信息列表成功！");
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("获取平台信息列表失败！");
            }
            return result;
        } else {
            List<PlatformPageResult> list = platformMapper.getPlatformList(platformname, platformintro, credit,
                    platformhour, begin, end, pagesize);
            if (list.size() > 0) {
                result.setTotal(list.get(0).getCnt());
                result.setPageCount(list.get(0).getPage());
                result.setPageSize(pagesize);
                result.setPageIndex(page);
                result.setRows(list);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("获取平台信息列表成功！");
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("获取平台信息列表失败！");
            }
            return result;
        }
    }

    @Override
    public PlatformResult getPlatformById(String platformid) {
        PlatformResult result = new PlatformResult();
        PlatformResult platform = platformMapper.getPlatformById(platformid);
        if (platform != null) {
            BeanUtils.copyProperties(platform, result);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取单个平台信息成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取单个平台信息失败！");
        }
        return result;
    }


}
